﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;

namespace GPS.Controllers
{
    public class GPS_DDSSHController : FDBController
    {
        //FDBContext db = new FDBContext();

        //public ApplicationUser getCurrentUser()
        //{
        //    ApplicationDbContext dbUser = new ApplicationDbContext();

        //    string userId = User.Identity.GetUserId();

        //    ApplicationUser _currentUser = dbUser.Users.Find(userId);

        //    return _currentUser;
        //}
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ActionResult Index(ViewModelDSH SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<DSH>(o => (SearchModel.pSH_MA == null || o.SH_MA.StartsWith(SearchModel.pSH_MA))
                                                             && (SearchModel.pSH_SOQD == null || o.SH_SOQD.Contains(SearchModel.pSH_SOQD))
                                                             && (SearchModel.pSH_DVGTVT_MA == null || o.SH_DVGTVT_MA == SearchModel.pSH_DVGTVT_MA)
                                                             && (SearchModel.pSH_NGAY_TU == null || (o.SH_NGAY >= SearchModel.pSH_NGAY_TU))
                                                             && (SearchModel.pSH_NGAY_DEN == null || (o.SH_NGAY <= SearchModel.pSH_NGAY_DEN))
                                                             );

            var model = new List<DSH>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DSH>(currentEntity));
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            return View(SearchModel);
        }

        public ActionResult Create()
        {
            initialCategoryCreateAction();

            return View(new CrudModelDSH());
        }

        [HttpPost]
        public ActionResult Create(CrudModelDSH model)
        {
            if (ModelState.IsValid)
            {

                DSH _model = new DSH();
                GPS.COMMON.Helpers.CopyObject<DSH>(model, ref _model, new List<string> { "SH_ACTIVE" });
                _model.SH_ACTIVE = (model.SH_ACTIVE == false ? "0" : "1");

                _db.Insert<DSH>(_model);

                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Edit(string id1, string id2)
        {
            string _ma = id1.ToString();
            string _madv = id2.ToString();
            DSH model = _db.GetOne<DSH>(o => o.SH_MA == id1 && o.SH_DVGTVT_MA == id2);

            CrudModelDSH _model = new CrudModelDSH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDSH>(model, ref _model, new List<string> { "SH_ACTIVE" });
            _model.SH_ACTIVE = (model.SH_ACTIVE == "0" ? false : true);

            initialCategoryEditAction(_model);
            return View(_model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDSH model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DSH>(o => o.SH_MA == model.SH_MA && o.SH_DVGTVT_MA == model.SH_DVGTVT_MA);
                GPS.COMMON.Helpers.CopyObject<DSH>(model, ref _model, new List<string> { "SH_ACTIVE" });

                _db.Update<DSH>(_model);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public JsonResult AutoCompleteHV_NOITT_DVHC_MA(string term)
        {
            var result = _db.Filter<SDONVIHC>(o => o.DVHC_MA.Contains(term));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult getHV_MA(string gv_DVGTVT_MA)
        {
            string strSH_MA = string.Empty;

            if (!string.IsNullOrWhiteSpace(gv_DVGTVT_MA))
            {
                var maxSH_MA = _db.Filter<DSH>(o => o.SH_DVGTVT_MA == gv_DVGTVT_MA).OrderByDescending(o => o.SH_MA).FirstOrDefault();
                var _year = DateTime.Now.Year.ToString().Substring(2, 2);
                strSH_MA = gv_DVGTVT_MA + "-K" + _year + "01";

                if (maxSH_MA != null)
                {
                    var last_1 = maxSH_MA.SH_MA.Substring(maxSH_MA.SH_MA.Length - 2, 2);
                    var curGV_MA = int.Parse(last_1) + 1;
                    var _year_max = maxSH_MA.SH_MA.Substring(maxSH_MA.SH_MA.Length - 4, 2);
                    if (_year_max == _year)
                    {
                        strSH_MA = gv_DVGTVT_MA + "-K" + _year_max + (curGV_MA < 10 ? "0" + curGV_MA.ToString() : curGV_MA.ToString());
                    }

                }

            }

            return Content(string.Join("", strSH_MA));
        }

        //auto complete:

        public JsonResult AutoCompleteDSSH_TTSH_MA(string term)
        {
            var result = _db.Filter<DSH>(o => o.SH_MA.Contains(term));
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public JsonResult AutoCompleteDSSH_HV_MA(string term)
        {
            var result = _db.Filter<DHV>(o => o.HV_MA.Contains(term));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

       
        public void initialCategorySearchAction(ViewModelDSH viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.pSH_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");

        }

        public void initialCategoryCreateAction()
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DSSH_CSDT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DSSH_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DSSH_SOHOSOs = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN");
            
        }

        public void initialCategoryEditAction(CrudModelDSH view)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DSSH_CSDT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DSSH_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DSSH_SOHOSOs = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN");

        }
	}
}